#!/bin/bash
set -e

# 检查是否有未提交的更改
if [[ -n $(git status -s) ]]; then
  echo "Error: There are uncommitted changes. Please commit or stash them first."
  exit 1
fi

# 更新版本号 (patch版本，如需其他类型可改为minor或major)
npm version patch --no-git-tag-version -m "chore: bump version to %s"

# 获取版本号并创建git tag
VERSION=$(node -p "require('./package.json').version")
git tag -a "v$VERSION" -m "Release v$VERSION"

# 获取当前分支名
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
if [[ -z "$BRANCH_NAME" ]]; then
  echo "Error: Unable to determine current branch."
  exit 1
fi

# 推送提交和标签到当前分支对应的远程分支
# 使用双引号防止通配符扩展和单词分割
git push origin "$BRANCH_NAME" --tags

# 发布到npm
npm publish
